/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.luban.account.main.service;

import cn.tedu.luban.account.common.enums.AccountResultEnum;
import cn.tedu.luban.account.main.converter.AccountConverter;
import cn.tedu.luban.account.main.repository.AccountRepository;
import cn.tedu.luban.account.protocol.bo.AccountBO;
import cn.tedu.luban.account.protocol.param.AccountCreateParam;
import cn.tedu.luban.account.protocol.param.AccountParam;
import cn.tedu.luban.account.protocol.param.AccountPayParam;
import cn.tedu.luban.account.protocol.query.AccountQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountAdminService {
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private AccountConverter adminConverter;

    public Long saveAccount(
        AccountCreateParam accountParam){
        return this.accountRepository.save(accountParam);
    }
    public Integer enableAccount(Long id) throws BusinessException {
        AccountBO account = accountRepository.getAccount(id);
        if (account.getStatus()==1){
            throw new BusinessException(AccountResultEnum.ACCOUNT_IS_ENABLE);
        }
        if (account.getSettlingAmount()>0){
            //结算已有金额
            AccountParam accountParam=adminConverter.bo2param(account);
            accountParam.setTotalAmount(account.getTotalAmount()+account.getSettlingAmount());
            accountParam.setSettlingAmount(0l);
            accountRepository.settleAccount(accountParam);
        }
        return this.accountRepository.enable(id);
    }

    public Integer disableAccount(Long id) throws BusinessException {
        return this.accountRepository.disable(id);
    }
    public ListRecordTotalBO<AccountBO> queryAccount(
        AccountQuery accountQuery) {
        Long totalRecord = this.accountRepository.getAccountCount(accountQuery);
        List<AccountBO> accountBoList = null;
        if (totalRecord > 0) {
            accountBoList = this.accountRepository.queryAccounts(accountQuery);
        }
        return new ListRecordTotalBO<>(accountBoList, totalRecord);
    }

    public AccountBO getAccount(Long userId){
        return this.accountRepository.getAccount(userId);
    }

    public Long pay(AccountPayParam param) {
        //如果賬戶是凍結狀態，將支付金額寫到待結算金額中，如果解凍的時候，在更新到total_amount
        AccountBO account = accountRepository.getAccountByUserId(param.getUserId());
        if (account.getStatus()==0){
            return accountRepository.updateSettlingAmount(param);
        }else {
            return accountRepository.updateTotalAmount(param);
        }
    }

    public AccountBO getAccountByUserId(Long userId) {
        return accountRepository.getAccountByUserId(userId);
    }
}